package com.book.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.business.entity.BookCart;
import com.book.business.mapper.BookCartMapper;
import com.book.business.service.BookCartService;
import com.book.common.entity.QueryRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @Author: 盛佳帅
 * @Description:  20173953
 */

@Service
public class BookCartServiceImpl extends ServiceImpl<BookCartMapper, BookCart> implements BookCartService {

    @Autowired
    BookCartMapper bookCartMapper;

    @Override
    public List<BookCart> findBookCart(BookCart bookCart) {
        QueryWrapper<BookCart> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bookCart.getPubid())) {
            queryWrapper.like("pubid", bookCart.getPubid());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<BookCart> pageBookCart(QueryRequest request, BookCart bookCart) {
        QueryWrapper<BookCart> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bookCart.getBookname())) {
            queryWrapper.like("bookname", bookCart.getBookname());
        }
        if (StringUtils.isNotEmpty(bookCart.getType())) {
            queryWrapper.like("type", bookCart.getType());
        }
        if (StringUtils.isNotEmpty(bookCart.getUsername())) {
            queryWrapper.like("username", bookCart.getUsername());
        }
        Page<BookCart> bookCartIPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(bookCartIPage, queryWrapper);
    }

    @Override
    public void addBookCart(BookCart bookCart) {
        this.save(bookCart);
    }

    @Override
    public void updateBookCart(BookCart bookCart) {
        this.saveOrUpdate(bookCart);
    }

    @Override
    public void deleteBookCart(String[] bookCart) {
        List<String> list = Arrays.asList(bookCart);
        this.removeByIds(list);
    }

    @Override
    public List<BookCart> payBookCart(String[] bookCart) {
        List<String> list = Arrays.asList(bookCart);
        return (List<BookCart>) this.listByIds(list);
    }
}